package database

import (
	"demo/models"
	"fmt"
)

func SearchUser(searchRequest *models.SearchUserRequest) ([]*models.UserProfile, int, error) {
	var users []*models.UserProfile
	session := engine.Table("user_profile").Limit(searchRequest.Limit, searchRequest.Offset)
	if searchRequest.Acc {
		session = session.Where("name = ?", searchRequest.Name)
	} else {
		session = session.Where("name like ?", "%"+searchRequest.Name+"%")
	}
	if searchRequest.OrderBy == "" {
		searchRequest.OrderBy = "id"
	}
	if searchRequest.Asc {
		fmt.Println(searchRequest.OrderBy)
		session = session.Asc(searchRequest.OrderBy)

	} else {
		session = session.Desc(searchRequest.OrderBy)
	}

	cnt, err := session.FindAndCount(&users)
	return users, int(cnt), err
}
